package b2.widget.tab
{
	import b2.config.*;
	import b2.util.*;
	import b2.widget.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class TabController extends Sprite
	{
		public var tabs:Array;
		public var tabTitles:Array;
		public var currentTabIndex:int;
		public var type:int;
		private var closeButton:BasicButton;
		private var tabTitlePosX:Number;
		private var tabTitlePosY:Number;
		private var tabPagePosX:Number;
		private var tabPagePosY:Number;
		private var tabSelectCallBack:Function;
				     
		public function TabController(BackgroundImage:Class = null, CloseButtonImage:Class = null, type:int = 0)
		{
			super();
			tabs = new Array();
			tabTitles = new Array();
			this.type = type;
			if (BackgroundImage != null)
			{
				var backgroundImage:Sprite = new BackgroundImage();
				addChild(backgroundImage);
			}
			if (CloseButtonImage != null)
			{
				//关闭按钮
				closeButton = new BasicButton(CloseButtonImage);
				closeButton.x = this.width - closeButton.width - 38;
				closeButton.y = 10;
				addChild(closeButton);
				closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			}
		}
		
		public function setCloseButtonPos(posX:Number, posY:Number):void
		{
			if (closeButton != null)
			{
				closeButton.x = posX;
				closeButton.y = posY;
			}
		}
		
		public function setTabTitlePos(posX:Number, posY:Number, gap:int = 5):void
		{
			this.tabTitlePosX = posX;
			this.tabTitlePosY = posY;
			for (var i:int = 0; i < tabTitles.length; i++)
			{
				tabTitles[i].x = tabTitlePosX + i * (tabTitles[i].width + gap);
				tabTitles[i].y = tabTitlePosY;
			}
		}
		
		public function setTabPagePos(posX:int, posY:int):void
		{
			this.tabPagePosX = posX;
			this.tabPagePosY = posY;
			for (var i:int = 0; i < tabs.length; i++)
			{
				tabs[i].x = tabPagePosX;
				tabs[i].y = tabPagePosY;
			}
		}
		
		public function removeAllTabs():void
		{
			while (tabTitles.length > 0)
			{
				var tabTitle:Sprite = tabTitles.pop();
				var tab:Sprite = tabs.pop();
				removeChild(tabTitle);
				removeChild(tab);
			}
			currentTabIndex = 0;
		}
		
		public function addTab(TabTitleBackground:Class, tabTitleName:String, tab:Sprite, HighlightTabBgr:Class = null):void
		{
			//标签
			if (type == 0)
			{
				var tabTitle:TabTitle = new TabTitle(TabTitleBackground, tabTitleName, tabs.length, HighlightTabBgr);
				tabTitle.x = tabTitlePosX + tabs.length * (tabTitle.width + 5);
				tabTitle.y = tabTitlePosY;
//				tabTitle.filters = [currentTabTileFilter];
				tabTitle.addEventListener(MouseEvent.CLICK, onMouseClick);
				tabTitle.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				tabTitle.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				tabTitles.push(tabTitle);
				addChild(tabTitle);
			}
			else
			{
				var tabIcon:TabIcon; 
				tabIcon = new TabIcon(TabTitleBackground, tabTitleName, tabs.length, true);
				tabIcon.x = tabTitlePosX + tabs.length * (tabIcon.width + 5);
				tabIcon.y = tabTitlePosY;
				tabIcon.addEventListener(MouseEvent.CLICK, onMouseClick);
				tabTitles.push(tabIcon);
				addChild(tabIcon);
			}
			//标签页
			tab.x = tabPagePosX;
			tab.y = tabPagePosY;
			if (tabs.length != currentTabIndex)
			{
				tab.visible = false;
			}
			else
			{
				tab.visible = true;
				if (type == 0)
					tabTitle.highlight = true;
			}
			tabs.push(tab);
			addChild(tab);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			var clickedTabTitle:TabTitle = event.currentTarget as TabTitle;
			selectTab(clickedTabTitle.index);
		}
		
		public function setTabSelectCallBack(callback:Function):void
		{
			this.tabSelectCallBack = callback; 	
		}
		
		public function selectTab(tabIndex:int):void
		{
			if (tabIndex != currentTabIndex)
			{
				tabs[currentTabIndex].visible = false;
				tabs[tabIndex].visible = true;
				(tabTitles[currentTabIndex] as TabTitle).highlight = false;
				(tabTitles[tabIndex] as TabTitle).highlight = true;
				(tabTitles[tabIndex] as TabTitle).filters = null;
				currentTabIndex = tabIndex;
				if (tabSelectCallBack != null)
					tabSelectCallBack();
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			var tabTitle:TabTitle = event.currentTarget as TabTitle;
			if (tabTitle.index != currentTabIndex)
			{
				tabTitle.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			}
			Mouse.cursor = "button";
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			var tabTitle:TabTitle = event.currentTarget as TabTitle;
			if (tabTitle.index != currentTabIndex)
			{
				tabTitle.filters = null;
			}
			Mouse.cursor = "auto";
		}

		private function onCloseButtonClick(event:MouseEvent):void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		
	}
}